package com.ruoyi.project.lely.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.R;
import com.ruoyi.common.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.lely.bean.entity.LyCollect;
import com.ruoyi.project.lely.bean.entity.LyGoods;
import com.ruoyi.project.lely.bean.entity.LyOrderGoods;
import com.ruoyi.project.lely.bean.vo.LyGoodsVO;
import com.ruoyi.project.lely.bean.vo.LyOrderGoodsVO;
import com.ruoyi.project.lely.bean.vo.LyOrderVO;
import com.ruoyi.project.lely.constant.CommonConstant;
import com.ruoyi.project.lely.constant.OrderConstant;
import com.ruoyi.project.lely.bean.entity.LyOrder;
import com.ruoyi.project.lely.mapper.LyOrderMapper;
import com.ruoyi.project.lely.service.LyOrderGoodsService;
import com.ruoyi.project.lely.service.LyOrderService;
import com.ruoyi.project.lely.bean.vo.LyUserCountVO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Desc: 订单信息
 * @Author LuoYong
 * @Date 2022/1/17 18:14
 */
@Slf4j
@Service
@DataSource(value = DataSourceType.SLAVE)
public class LyOrderServiceImpl extends ServiceImpl<LyOrderMapper, LyOrder> implements LyOrderService {

    @Autowired
    private LyOrderGoodsService orderGoodsService;

    /**
     * 用户订单统计
     *
     * @param userId
     * @return
     */
    @Override
    public LyUserCountVO getOrderAccountById(Long userId) {
        List<LyOrder> list = this.list(new LambdaQueryWrapper<LyOrder>().eq(LyOrder::getUserId, userId));
        if (CollUtil.isNotEmpty(list)) {
            AtomicInteger unpaid = new AtomicInteger(0);        //待支付
            AtomicInteger unship = new AtomicInteger(0);        //待发货
            AtomicInteger unrecv = new AtomicInteger(0);        //待收货
            AtomicInteger uncomment = new AtomicInteger(0);     //待评价
            Map<Integer, List<LyOrder>> map = list.stream().collect(Collectors.groupingBy(LyOrder::getOrderStatus));
            map.forEach((k, v) -> {
                if (OrderConstant.STATUS_CREATE.equals(k)) {
                    unpaid.set(v.size());
                } else if (OrderConstant.STATUS_PAY.equals(k)) {
                    unship.set(v.size());
                } else if (OrderConstant.STATUS_SHIP.equals(k)) {
                    unrecv.set(v.size());
                } else if (OrderConstant.STATUS_CONFIRM.equals(k) || OrderConstant.STATUS_AUTO_CONFIRM.equals(k)) {
                    uncomment.set(v.size());
                } else {
                    log.info("其它状态，不做处理");
                }
            });
            return LyUserCountVO.builder()
                    .unpaid(unpaid.get())
                    .unship(unship.get())
                    .unrecv(unrecv.get())
                    .uncomment(uncomment.get())
                    .build();
        }
        return new LyUserCountVO();
    }

    /**
     * 查询订单详情
     * @param page
     * @param showType
     * @return
     */
    @Override
    public R getOrderDetail(Page<LyOrder> page, Integer showType) {
        Long userId = SecurityUtils.getUserId();
        Page list = this.getBaseMapper().selectPage(
                page,
                new LambdaQueryWrapper<LyOrder>()
                        .eq(LyOrder::getUserId, userId)
                        .eq(ObjectUtil.equal(showType,1), LyOrder::getOrderStatus, OrderConstant.STATUS_CREATE)
                        .eq(ObjectUtil.equal(showType,2), LyOrder::getOrderStatus, OrderConstant.STATUS_PAY)
                        .eq(ObjectUtil.equal(showType,3), LyOrder::getOrderStatus, OrderConstant.STATUS_SHIP)
                        .in(ObjectUtil.equal(showType,4), LyOrder::getOrderStatus, OrderConstant.STATUS_CONFIRM,OrderConstant.STATUS_AUTO_CONFIRM)
                        .eq(LyOrder::getDeleted, CommonConstant.DELETED)
        );
        List<LyOrderVO> vos = new ArrayList<>();
        List<LyOrder> records = list.getRecords();
        if(CollUtil.isNotEmpty(records)){
            records.stream().forEach(n -> {
                LyOrderVO vo = new LyOrderVO();
                BeanUtils.copyProperties(n,vo);
                List<LyOrderGoods> orderGoodsList = orderGoodsService.list(
                        new LambdaQueryWrapper<LyOrderGoods>()
                                .eq(LyOrderGoods::getOrderId,n.getId())
                                .eq(LyOrderGoods::getDeleted, CommonConstant.DELETED)
                );
                List<LyOrderGoodsVO> goodsVOS = new ArrayList<>();
                if(CollUtil.isNotEmpty(orderGoodsList)){
                    orderGoodsList.stream().forEach(m -> {
                        LyOrderGoodsVO orderGoodsVO = new LyOrderGoodsVO();
                        BeanUtils.copyProperties(m,orderGoodsVO);
                        goodsVOS.add(orderGoodsVO);
                    });
                }
                vo.setGoodsList(goodsVOS);
                vos.add(vo);
            });
        }
        list.setRecords(vos);
        return R.ok(list);
    }
}
